<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="学号/教师号" prop="id">
        <el-input v-model.trim="form.id" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="账号" prop="username">
        <el-input v-model.trim="form.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model.trim="form.password"
          type="password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="displayName">
        <el-input v-model.trim="form.displayName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model.trim="form.email" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="tel">
        <el-input v-model.trim="form.tel" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <!-- <el-input v-model.trim="form.gender" autocomplete="off"></el-input> -->
        <el-select v-model="form.gender" placeholder="请选择">
          <el-option
            v-for="item in genderOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="角色" prop="roles">
        <el-select v-model="form.roles" multiple placeholder="请选择">
          <el-option
            v-for="item in roleOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="注册时间" prop="entryDate">
        <el-date-picker
          v-model="form.entryDate"
          type="date"
          style="width: 100%"
          value-format="timestamp"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>

      <!-- <el-form-item label="头像" prop="avatar">
        <el-input v-model="form.avatar"></el-input>
      </el-form-item>

      <el-form-item label>
        <el-avatar :size="50">
          <img :src="form.avatar" />
        </el-avatar>
        <upload-avatar :url.sync="form.avatar"></upload-avatar>
      </el-form-item> -->
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doSave } from '@/api/userManagement'
  import { baseAvatarURL } from '@/config'

  export default {
    name: 'UserManagementEdit',
    components: {},

    data() {
      return {
        form: {
          username: '',
          displayName: '',
          password: '',
          entryDate: new Date().getTime(),
          email: '',
          phone: '',
          avatar: baseAvatarURL,
          roles: [],
        },
        rules: {
          username: [
            { required: true, trigger: 'blur', message: '请输入账号' },
          ],
          displayName: [
            { required: true, trigger: 'blur', message: '请输入用户名' },
          ],
          password: [
            { required: false, trigger: 'blur', message: '请输入密码' },
          ],
          email: [{ required: false, trigger: 'blur', message: '请输入邮箱' }],
          phone: [
            { required: false, trigger: 'blur', message: '请输入手机号' },
          ],
          roles: [{ required: true, trigger: 'blur', message: '请选择角色' }],
        },
        title: '',
        dialogFormVisible: false,
        isAdd: false,
        roleOptions: [
          {
            value: 'admin',
            label: '管理员',
          },
          {
            value: 'teacher',
            label: '教师',
          },
          {
            value: 'student',
            label: '学生',
          },
        ],
        genderOptions: [
          {
            value: '男',
            label: '男',
          },
          {
            value: '女',
            label: '女',
          },
          {
            value: '未设置',
            label: '未设置',
          },
        ],
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
          this.isAdd = true
        } else {
          this.isAdd = false
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            // 此处添加
            const { msg } = await doSave(this.form)

            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
